﻿using DnD.Common;
using DnD.Model;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace DnD.ViewModels
{
    class GameSetupViewModel : INotifyPropertyChanged
    {

        public ObservableCollection<Deck> Decks { get; set; }
        public DelegateCommand StartGameCommand { get; set; }


        private Deck _selectedDeck;
        private PhoneApplicationPage _view;
 

        public Deck SelectedDeck
        {
            get { return _selectedDeck; }
            set
            {
                _selectedDeck = value;
                OnPropertyChanged();
            }
        }



        public GameSetupViewModel(PhoneApplicationPage view)
        {

            _view = view;

            using (var context = new DnDDataContext(DnDDataContext.DbConnectionString))
            {
                Decks = new ObservableCollection<Deck>(context.GetTable<GameItem>().OfType<Deck>());
            }

            SelectedDeck = Decks[0];


            StartGameCommand = new DelegateCommand(startGame, deckID => SelectedDeck != null);

        }

        private void startGame(object deckID)
        {
            _view.NavigationService.Navigate(new Uri("/GamePage.xaml?deckID=" + SelectedDeck.Id, UriKind.Relative));

        }

        public void handleMenuRequest(String menuItemName)
        {
            if (menuItemName == "pin")
            {
                StandardTileData tileData = new StandardTileData
                {
                    Title = "DnD QuickGame",
                    BackgroundImage = new Uri(SelectedDeck.PictureURL, UriKind.Absolute),
                    BackTitle = SelectedDeck.Name,
                    BackBackgroundImage = new Uri("", UriKind.Relative),
                    BackContent = SelectedDeck.Description
                };

                string tileUri = string.Concat("/GamePage.xaml?deckID=", SelectedDeck.Id);
                ShellTile.Create(new Uri(tileUri, UriKind.Relative), tileData);
            }
           
        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }

    }
}
